from db_connect import ConnMysqlSlave
from db_connect import ConnMysql
import aiomysql


class Product():

    def __init__(self, product_name, product_price, product_image, product_type_id):

        self.product_name = product_name
        self.product_price = product_price
        self.product_image = product_image
        self.product_type_id = product_type_id
        # self.result = self.main()




    async def result(self):

        #判断所传参数是否为空
        if not (self.product_name and self.product_price and self.product_image and self.product_type_id):

            return {
                'status': -1,
                'errorCode': 701,
                'errorMsg': '参数不能为空',
                'resultBody': None,
            }

        #查找product表中是否有需要传的参数
        async with ConnMysqlSlave().pool.acquire() as c:

            async with c.cursor(aiomysql.DictCursor) as cur:
                sql = "SELECT * FROM product WHERE product_name=%s AND product_price=%s AND product_image=%s"
                await cur.execute(sql, (self.product_name, self.product_price, self.product_image))
                select_info = await cur.fetchone()
                print(select_info)

        #如果没有找到，把参数插到表里
        if not select_info:

            async with ConnMysql().pool.acquire() as c:

                async with c.cursor(aiomysql.DictCursor) as cur:

                    sql = "INSERT INTO product(product_name, product_price, product_image) VALUES (%s, %s, %s)"
                    await cur.execute(sql, (self.product_name, self.product_price, self.product_image))
                    await c.commit()


                    sql_ = "SELECT * FROM product WHERE product_name=%s AND product_price=%s AND product_image=%s"
                    await cur.execute(sql_, (self.product_name, self.product_price, self.product_image))
                    select_info = await cur.fetchone()

        #通过插入表里的数据，查到product_id
        product_id = select_info['id']
        print(product_id)

        #查找intermediate_table表中是否已经存在所传参数
        async with ConnMysqlSlave().pool.acquire() as c:

            async with c.cursor(aiomysql.DictCursor) as cur:
                sql = '''SELECT * FROM intermediate_table WHERE product_id =%s AND product_type_id=%s'''
                await cur.execute(sql, (product_id, self.product_type_id))
                select_info=await cur.fetchone()
                print(select_info)

        #如果没有找到，将查找到的product_id和商品对应的product_type_id插入intermediate_table表里
        if not select_info:

            async with ConnMysql().pool.acquire() as c:

                async with c.cursor(aiomysql.DictCursor) as cur:

                    sql = '''INSERT INTO intermediate_table (product_id, product_type_id) VALUES (%s, %s)'''

                    await cur.execute(sql, (product_id, self.product_type_id))
                    await c.commit()


        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': 'OK',
        }